/*
 * Copyright (c) 2009-2010 HIT Microelectronic Center
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Gou Pengfei
 * Date:    Feb. 2011
 *
 */

#ifndef __CPU_EDGE_PIECEWISE_EXITP_HH__
#define __CPU_EDGE_PIECEWISE_EXITP_HH__

#include <vector>

#include "base/types.hh"
#include "config/the_isa.hh"
#include "arch/isa_traits.hh"
#include "cpu/edge/pred/piecewise.hh"
#include "params/DerivEdgeCPU.hh"

class DerivEdgeCPUParams;

template<class Impl>
class PiecewiseExitP
{
  public:
    typedef TheISA::BlockID BlockID;
    typedef TheISA::ExitID ExitID;

    enum HistoryMode {
        ExitHistory,
        SingleBitHistory
    };

    PiecewiseExitP(DerivEdgeCPUParams *params);

    ~PiecewiseExitP();

    void reset();

    /** Lookup the predictor to produce a prediction. */ 
    TheISA::ExitID lookup(BlockID block_id, BlockID oldest_block_id,
            Addr &block_PC, ThreadID tid);

    /** Non-speculative update the weight tables. */
    void update(BlockID block_id, Addr &block_PC, ExitID actual_exit_id,
            ExitID pred_exit_id, ThreadID tid);

    /** Squash due to other reasons but branch misprediction. */
    void squash(BlockID block_id, ThreadID tid);

    /** Squash due to branch misprediction. */
    void squash(Addr block_pc, BlockID block_id, ExitID actual_exit_id,
            ExitID pred_exit_id, ThreadID tid);

  private:

    /** History mode of this predictor. */
    HistoryMode historyMode;

    /** Generate exit_id by the outputs of the three predictors. */
    TheISA::ExitID exitGen(bool msb, bool mb, bool lsb);

    /** Piecewise predictor for the most significant bit of exit id. */
    PiecewiseBP<Impl> * msbPredictor;

    /** Piecewise predictor for the middle bit of exit id. */
    PiecewiseBP<Impl> * mbPredictor;

    /** Piecewise predictor for the least significant bit of exit id. */
    PiecewiseBP<Impl> * lsbPredictor;

    /**
     * The number of bits of history. This is the size of third
     * dimension of the weight table.
     * */
    int msbHistoryBits;
    int mbHistoryBits;
    int lsbHistoryBits;

    /**
     * The number of entries of the second dimension. This is indexed
     * by the path-based address of previously executed block.
     * */
    int msbPathTableSize;
    int mbPathTableSize;
    int lsbPathTableSize;

    /**
     * The number of entries of the third dimension. This is indexed
     * by PC of the currently predicting block.
     * */
    int msbTableSize;
    int mbTableSize;
    int lsbTableSize;

    /**
     * The number of bits of local history.
     * */
    int msbLocalHistoryBits;
    int mbLocalHistoryBits;
    int lsbLocalHistoryBits;

    /**
     * The number of entries of local history table.
     * */
    int msbLocalHistoryTableSize;
    int mbLocalHistoryTableSize;
    int lsbLocalHistoryTableSize;

    /** The number of bits of each weight. */
    unsigned weightBits;

    /** The number of bits to align. */
    unsigned blockShiftAmt;
};

#endif // __CPU_EDGE_PIECEWISE_EXITP_HH__
